package com.ning.cloud.common.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

import java.util.List;

@Configuration
public class SwaggerConfig {

    @Value("${spring.application.name}")
    private String applicationName;

    // 可以从配置文件中注入值
    @Value("${knife4j.setting.enable-host-text:http://localhost:9999}")
    private String serverUrl;

    @Value("${knife4j.setting.enable-host-description:Gateway Server}")
    private String serverDescription;

    @Bean
    public OpenAPI springDocOpenAPI() {
        String title = String.format("服务：[%s] swagger文档", this.applicationName);
        Server server = new Server()
                .url(serverUrl)
                .description(serverDescription);

        return new OpenAPI()
                .servers(List.of(server))
                .info(new Info().title(title).description("Swagger3 Spring Boot 3.0 application")
                        .version("v0.0.1"))
                //文档添加头部Authorization
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION,new SecurityScheme()
                                .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP).scheme("bearer")))
                        .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

    /**以下分组和资源映射都可省略*/
//    @Bean
//    public GroupedOpenApi userService() {
//        return GroupedOpenApi.builder().group("user-service模块")
//                .pathsToMatch("/user/**")
//                .build();
//    }


}